<?php

namespace app\api\controller;

use app\api\extend\Common;
use app\api\prot\Result;

class Video extends Common implements Result
{
    public function add() {
        $data = $this->check_params();
        if ($this->isJson($data)) return $data;

        $video_src = $this->uploadFile('uploads/video','src','图片');
        if ($this->isJson($video_src)) return $video_src;
        
        $data['video_src'] = implode(", ",$video_src);
        $res = $this->model->add($data);
        if (!$res){
            return $this->return_msg(400,$this->getConst(false));
        }
        return $this->return_msg(200,$this->getConst());
    }

    public function list() {
        $data = [];
        $id = $this->params['id']??null;
        $uid = $this->params['uid']??null;

       if($id && $uid){
           $data['video_id']=$id;
           $data['video_uid']=$uid;
        } elseif ($id){
           $data['video_id']=$id;
       }elseif($uid){
           $data['video_uid']=$uid;
       }
        $res = $this->model->list($data);
        if (!$res){
            return $this->return_msg(400,$this->getConst(false));
        }
        return $this->return_msg(200,$this->getConst(),$res->toArray());
    }

    public function del()
    {
        $data = $this->check_params();
        if ($this->isJson($data)) return $data;

        $data = [
            'video_id'=>$data['video_ids']??'',
            'video_uid'=>$data['video_uid']??'',
        ];
        $res = $this->model->del($data);
        if (!$res){
            return $this->return_msg(400,$this->getConst(false));
        }
        return $this->return_msg(200,$this->getConst(),['count'=>$res],false);
    }

    public function upd()
    {
        $data = $this->check_params();
        if ($this->isJson($data)) return $data;

        $newData = [
            'video_id'=>$data['video_id']??'',
            'video_uid'=>$data['video_uid']??'',
        ];
        $newData['data'] = array_diff_key($data,array_keys($newData));
        $video_src = $this->uploadFile('uploads/video','src','视频');
        if ($this->isJson($video_src)) {
            $newData['data']['video_src'] = $video_src;
        }
        $res = $this->model->upd($newData);
        if (!$res){
            return $this->return_msg(400,$this->getConst(false));
        }
        return $this->return_msg(200,$this->getConst(),['count'=>$res],false);
    }
}